const db = wx.cloud.database()
var util = require('../../utils/util.js')
Page({
  data: {
    title: '',
    description: '',
    voteoptions: [{
        option: ''
      },
      {
        option: ''
      }
    ],
    optionnum: 2,
    votetype: '单选',
    date: '2019-11-15',
    picker: ['单选'],
    index: 0,
    announymous: true
  },

  onShow: function() {
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo == "") {
      wx.navigateTo({
        url: '../auth/auth'
      })
    } else {
      this.setData({
        userInfo: userInfo
      })
      var todaydate = util.formatDatastring(new Date())
      this.setData({
        date: todaydate
      })
    }
  },

  addoptions: function(event) {
    var optionnum = this.data.optionnum
    var voteoptions = this.data.voteoptions
    var voteoption1 = {}
    voteoption1.option = ""
    voteoptions[optionnum] = voteoption1
    this.setData({
      voteoptions: voteoptions,
      optionnum: optionnum + 1
    })
  },

  minusoptions: function(event) {
    console.log(event.currentTarget.dataset.id)
    var id = event.currentTarget.dataset.id
    var optionnum = this.data.optionnum
    var voteoptions = this.data.voteoptions
    voteoptions.splice(id, 1)
    this.setData({
      voteoptions: voteoptions,
      optionnum: optionnum - 1
    })
  },

  inputtitle: function(event) {
    this.setData({
      title: event.detail.value
    })
  },
  inputdescription: function(event) {
    this.setData({
      description: event.detail.value
    })
  },

  inputoptions: function(event) {
    var id = event.currentTarget.dataset.id
    var buff = event.detail.value
    var voteoptions = this.data.voteoptions
    voteoptions[id].option = buff
    console.log(voteoptions)
    this.setData({
      voteoptions: voteoptions
    })
  },

  PickerChange(e) {
    console.log(e);
    var picker = this.data.picker
    var index = e.detail.value
    this.setData({
      index: index,
      votetype: picker[index]
    })
  },

  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },

  switchchange: function(event) {
    this.setData({
      announymous: event.detail.value
    })
  },

  submit: function(event) {
    let userInfo = this.data.userInfo
    var date = this.data.date
    var time = this.data.time
    var deadline = new Date(date + " " + '23:00')
    var optionname = ['A', 'B', 'C', 'D', 'E', 'F']
    var voteoptions = this.data.voteoptions
    var votenum = {}
    if (voteoptions.length > 6) {
      wx.showToast({
        title: '选项不能超过6个',
        icon: 'success',
        duration: 2000
      })
      return
    } else {
      for (var index in voteoptions) {
        voteoptions[index].name = optionname[index]
        voteoptions[index].num = 0
        votenum[optionname[index]] = 0
      }
      db.collection('vote').add({
          data: {
            publisher: userInfo.nickName,
            avatarUrl: userInfo.avatarUrl,
            title: this.data.title,
            description: this.data.description,
            voteoptions: voteoptions,
            votetype: this.data.votetype,
            submitdate: db.serverDate(),
            deadline: deadline,
            announymous: this.data.announymous,
            available: true
          }
        })
        .then(res => {
          wx.showToast({
            title: '投票添加成功！',
            icon: 'success',
            duration: 2000
          })
          wx.reLaunch({
            url: '../home/home'
          })
        })
    }
  }
})